package com.demo.core.schedule;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import io.quarkus.scheduler.Scheduled;

import java.util.concurrent.TimeUnit;

public class TestSchedule {

    static boolean everyByDelayIsFirst = true;

    static boolean everyByDelayExpression = true;

    @Scheduled(cron = "0 0/1 * * * ?")
    public void testCorn() {
        System.out.println("cron表达式每分钟触发,当前时间:" + DateUtil.date().toString(DatePattern.NORM_DATETIME_FORMAT));
    }

    @Scheduled(every = "1m")
    public void every() {
        System.out.println("每间隔1分钟触发定时任务" + DateUtil.date().toString(DatePattern.NORM_DATETIME_FORMAT));
    }

    @Scheduled(every = "1m", delay = 5, delayUnit = TimeUnit.SECONDS)
    public void everyByDelay() {
        if (everyByDelayIsFirst) {
            System.out.println("首次触发系统启动后5秒后触发");
            everyByDelayIsFirst = false;
            return;
        }
        System.out.println("该任务为首次触发为延时触发后,每1分钟触发定时任务");
    }

    @Scheduled(every = "2m", delayed = "10s")
    public void everyByDelayExpression() {
        if (everyByDelayExpression) {
            System.out.println("首次触发系统启动后10秒后触发");
            everyByDelayExpression = false;
            return;
        }
        System.out.println("该任务为首次触发为延时触发后,每2分钟触发定时任务");
    }

    @Scheduled(cron = "0 0/2 * * * ?",skipExecutionIf = MySkipPredicate.class)
    public void conditionProcess() {
        System.out.println("定时任务执行条件通过,每2分钟执行一次");
    }
}
